import { Component, Input, Output, EventEmitter } from '@angular/core'
import { JsonApiService } from '../../core/api/json-api.service'

interface NodeCheck {
  isAdopt: String
  nodeName?: ''
}

interface ExamineInfoItem {
  name: String
  type: String
  exmaineOpinion: String
  nodes: NodeCheck
}

@Component({
  selector: 'sa-record-modal',
  templateUrl: './record-modal.component.html',
  styleUrls: ['./record-modal.css']
})
export class RecordModalComponent {
  @Input() checkType
  @Input() deviceInstallExamine
  @Input() car
  @Input() isDisable = false
  TcarDeatailUseList = carTypeListObj.carDeatailUseList
  imageList = []
  currentImageUrl = ''
  carInfo = {}
  currentSwitchType = ''
  examineInfoList = {}
  rightIcon = require('./icon/right.png')
  errorIcon = require('./icon/error.png')

  drivingLicense = {} // 行驶证资料

  constructor(private jsonApiService: JsonApiService) {}

  ngOnInit() {
    this.switchTab('1')
    this.getImageList()

    this.getDrivingLicense()
  }

  getImageList() {
    const arr = []
    for (const item of this.deviceInstallExamine.imageInfo) {
      for (const imageItem of item.imageList) {
        const obj = {
          index: item.index,
          url: imageItem
        }
        arr.push(obj)
      }
    }
    this.imageList = arr
    this.currentImageUrl = arr.length?arr[0].url : ''
  }

  getCarInfor() {
    return this.TcarDeatailUseList[this.car.useTypeL2]
  }

  radioChange(i, state, nodeName) {
    var className1, className2
    if (state == '1') {
      className1 = 'greenBtn'
      className2 = ''
    } else if (state == '2') {
      className1 = ''
      className2 = 'redBtn '
    }

    this.examineInfoList[this.currentSwitchType].nodes[i].isAdopt = state
    this.examineInfoList[this.currentSwitchType].nodes[i].nodeName = nodeName
    this.examineInfoList[this.currentSwitchType].nodes[i].className1 = className1
    this.examineInfoList[this.currentSwitchType].nodes[i].className2 = className2
  }

  @Output() submitSuccess = new EventEmitter()
  submit() {
    const submitArr = []
    for (const item in this.examineInfoList) {
      submitArr.push(this.examineInfoList[item])
    }
    this.jsonApiService
      .json_http('deviceInstallExamine/updateDeviceInstallExamine.html?id=' + this.deviceInstallExamine.id, submitArr)
      .subscribe(res => {
        console.log(res.message)
        this.jsonApiService.message(res.message)
        if (res.success) this.submitSuccess.emit()
      })
  }

  showInit() {
    this.switchTab('1')
  }

  switchTab(type) {
    $('#myTab2')
      .find('li')
      .eq(parseInt(type) - 1)
      .addClass('active')
      .siblings()
      .removeClass('active')
    this.currentSwitchType = type
    let isExist = false
    for (const item of this.imageList) {
      if (item.index == type) {
        this.currentImageUrl = item.url || ''
        isExist = true
        break
      }
    }
    if (!isExist) {
      this.currentImageUrl = ''
    }
    if (!Object.keys(this.examineInfoList).length) {
      for (const item of this.deviceInstallExamine.examineInfoList) {
        for (const node of item.nodes) {
          let className1 = ''
          let className2 = ''
          if (node.isAdopt === 1) {
            className1 = 'greenBtn'
            className2 = ''
          } else if (node.isAdopt === 2) {
            className1 = ''
            className2 = 'redBtn '
          }
          node.className1 = className1
          node.className2 = className2
        }
        this.examineInfoList[item.type.toString()] = item
      }
    }
  }

  openCheckWindow(dom) {
    dom.show()
  }

  closeCheckWindow(dom) {
    dom.hide()
  }

  // 获取行驶证资料
  getDrivingLicense() {
    this.jsonApiService
      .http_get('carDrivingLicense/queryCarDrivingLicenseByCarId.html?carId=' + this.car.id)
      .subscribe(res => {
        this.drivingLicense = res.data
      })
  }
}
